package com.troyhigh.njrotc.admintracker;

import java.io.Serializable;
import java.util.Arrays;

/**
 * A class to store information about a cadet, including ribbon records.
 * 
 * @author Quan Nguyen
 * @version 2008-December-21
 */
public class Cadet implements Serializable
{
    private static final long serialVersionUID = 5769291405710338701L; //<-- that's an "L" at the end.
    
    private int platoon;
    private String lastName;
    private String firstName;
    private int id;
    private char gender;
    private int grade;
    private int nsyear;
    private int[] ribbons;
    private int[] semester;

    //This class is meant more for retrieval purpose rather than to modify.
    //If I need to change a student, I'll change it by making a new one.
    public Cadet(int p, String l, String f, int i, char g, int r, int n, int[] a, int[] s)
    {
        platoon = p;
        lastName = l;
        firstName = f;
        id = i;
        gender = g;
        grade = r;
        nsyear = n;
        ribbons = (a != null ? a : new int[25]);
        semester = (s != null ? s : new int[25]);
    }
    
    public Cadet(int p, String l, String f, int i, char g, int r, int n)
    {
        this(p, l, f, i, g, r, n, new int[25], new int[25]);
    }
    
    //Your run of the mill accessor methods are here
    public int getPlatoon() { return platoon; }
    public String getLastName() { return lastName; }
    public String getFirstName() { return firstName; }
    public int getID() { return id; }
    public char getGender() { return gender; }
    public int getGrade() { return grade; }
    public int getNSYear() { return nsyear; }
    
    public String getName() { return lastName + ", " + firstName; }
    
    @Override
    public String toString() { return getID() + ": " + getName(); }
    
    @Override
    public boolean equals(Object s)
    {
        Cadet that = ((Cadet)s);
        return this.platoon == that.platoon &&
               this.lastName.equals(that.lastName) &&
               this.firstName.equals(that.firstName) &&
               this.id == that.id &&
               this.gender == that.gender &&
               this.grade == that.grade &&
               this.nsyear == that.nsyear &&
               Arrays.equals(this.ribbons, that.ribbons) &&
               Arrays.equals(this.semester, that.semester);
    }
    
    @Override
    public int hashCode()
    {
        return getName().hashCode() +
               platoon * 16 +
               id * 32 +
               gender * 64 +
               grade * 128 +
               nsyear * 256 +
               Arrays.hashCode(ribbons) +
               Arrays.hashCode(semester);
    }
    
    public int[] getRibbons()
    {
        return ribbons;
    }
    
    public int[] getSemester()
    {
        return semester;
    }
    
    public void setRibbons(int[] arr)
    {
        if(arr != null && arr.length == 25) ribbons = arr;
    }
    
    public void setSemester(int[] arr)
    {
        if(arr != null && arr.length == 25) semester = arr;
    }
    
    public void setRibbon(int index, int value)
    {
        if(index >= 0 && index < 25)
            ribbons[index] = value;
    }
    
    public void setSemester(int index, int value)
    {
        if(index >= 0 && index < 25)
            semester[index] = value;
    }
    
    public int getRibbon(int index)
    {
        if(index >= 0 && index < 25)
            return ribbons[index];
        return 0;
    }
    
    public int getSemester(int index)
    {
        if(index >= 0 && index < 25)
            return semester[index];
        return 0;
    }
    
    public int getRibbonByCategory(Category c)
    {
        switch(c)
        {
            case AA:
                return ribbons[12];
            case PT:
                return ribbons[14];
            case PA:
                return ribbons[15];
            case US:
                return ribbons[16];
            case CS:
                return ribbons[17];
            case DT:
                return ribbons[18];
            case CG:
                return ribbons[19];
            case RT:
                return ribbons[20];
            case OI:
                return ribbons[21];
            case BL:
                return ribbons[23];
            case SC:
                return ribbons[24];
            default:
                return 0;
        }
    }
    
    public int getSemesterByCategory(Category c)
    {
        switch(c)
        {
            case AA:
                return semester[12];
            case PT:
                return semester[14];
            case PA:
                return semester[15];
            case US:
                return semester[16];
            case CS:
                return semester[17];
            case DT:
                return semester[18];
            case CG:
                return semester[19];
            case RT:
                return semester[20];
            case OI:
                return semester[21];
            case BL:
                return semester[23];
            case SC:
                return semester[24];
            default:
                return 0;
        }
    }
    
    public String ribbonValues() //Database method only, no other purpose...
    {
        String result = "";
        for(int i = 0; i < ribbons.length; i++)
        {
            result += ribbons[i] + ", ";
        }
        return result.substring(0, result.length() -2);
    }
    
    public String semesterValues() //Database method only, no other purpose...
    {
        String result = "";
        for(int i = 0; i < semester.length; i++)
        {
            result += semester[i] + ", ";
        }
        return result.substring(0, result.length() -2);
    }
}